package com.ego.service.impl;

import com.ego.dubbo.service.ItemParamDubboService;
import com.ego.exception.DaoException;
import com.ego.pojo.EasyUIDatagrid;
import com.ego.pojo.EgoResult;
import com.ego.pojo.ItemParam;
import com.ego.service.ItemParamService;
import com.ego.utils.IDUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ItemParamServiceImpl implements ItemParamService {

    @DubboReference
    private ItemParamDubboService itemParamDubboService;

    @Override
    public EasyUIDatagrid selectItemByPage(int pageNum, int pageSize) {
        List<ItemParam> itemParams = itemParamDubboService.selectItemParamByPage(pageNum, pageSize);
        long total = itemParamDubboService.selectItemParamTotal();
        return new EasyUIDatagrid(itemParams, total);
    }

    @Override
    public EgoResult selectItemParamByItemCatId(Long itemCatId) {
        ItemParam itemParam = itemParamDubboService.selectItemParamByItemCatId(itemCatId);
        if (itemParam != null) {
            return EgoResult.ok(itemParam);
        }
        return EgoResult.error("商品规格查询失败");
    }

    @Override
    public EgoResult saveItemParam(long itemCatId, String paramData) {
        Date date = new Date();
        ItemParam itemParam = new ItemParam();
        itemParam.setItemCatId(IDUtils.genItemId());
        itemParam.setParamData(paramData);
        itemParam.setItemCatId(itemCatId);
        itemParam.setUpdated(date);
        itemParam.setCreated(date);
        int i = itemParamDubboService.saveItemParam(itemParam);
        if (i == 1) {
            return EgoResult.ok();
        }
        return EgoResult.error("新增商品规格失败");
    }

    @Override
    public EgoResult deleteItemParam(Long[] ids){
        try {
            int i = itemParamDubboService.deleteItemParam(ids);
            if (i == 1) {
                return EgoResult.ok();
            }
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return EgoResult.error("商品规格删除失败");
    }

}
